import {
  createWebHistory,
  createRouter,
  createWebHashHistory,
} from 'vue-router'
import Home from './views/Home.vue'
import Login from './views/Login.vue'
import ColumnDetail from './views/ColumnDetail.vue'
import Resign from './views/Resign.vue'
import CreatePost from './views/CreatePost.vue'
import EditInfomation from './views/EditInfomation.vue'
import PostDetail from './views/PostDetail.vue'
import A from './views/aaa.vue'
import Try from './views/aaaaa111.vue'
import Tip from './views/Tip.vue'
import { useMainStore } from '@/stores/main'
import http from '@/api/axios'
import { getCurrentUser } from '@/api/api'
// const routerHistory = createWebHistory()
const routerHistory = createWebHashHistory()
const router = createRouter({
  history: routerHistory,
  routes: [
    {
      path: '/aaa',
      name: 'aaa',
      component: A,
    },
    {
      path: '/try',
      name: 'try',
      component: Try,
    },
    {
      path: '/tip',
      name: 'tip',
      component: Tip,
    },
    {
      path: '/',
      name: 'home',
      component: Home,
    },
    {
      path: '/login',
      name: 'login',
      component: Login,
      meta: { redirectAlreadyLogin: true },
    },
    {
      path: '/column/:id',
      name: 'column',
      component: ColumnDetail,
    },
    {
      path: '/createpost',
      name: 'createPost',
      component: CreatePost,
      meta: { reuqiredLogin: true },
    },
    {
      path: '/resign',
      name: 'resign',
      component: Resign,
    },
    {
      path: '/editInfo',
      name: 'editInfo',
      component: EditInfomation,
    },
    {
      path: '/post/:id',
      name: 'post',
      component: PostDetail,
    },
    {
      path: '/:catchAll(.*)',
      name: 'notFound',
      component: () => import('./views/NotFound.vue'), // 懒加载???
    },
  ],
})

// 根据登录状态匹配功能
// router.beforeEach((to, from, next) => {
//   const { isLogin } = store.state.user
//   const { token } = store.state
//   const { reuqiredLogin, redirectAlreadyLogin } = to.meta
//   if (!isLogin) {
//     if (token) {
//       http.defaults.headers.common.Authorization = `Bearer ${token}`
//       getCurrentUser()
//         .then(res => {
//           store.fetchUserAction(res)
//           if (redirectAlreadyLogin) {
//             next('/')
//           } else {
//             next()
//           }
//         })
//         .catch(e => {
//           console.log(e)
//           store.logoutAction()
//           next('/login')
//         })
//     } else {
//       if (reuqiredLogin) {
//         next('/login')
//       } else {
//         next()
//       }
//     }
//   } else {
//     if (redirectAlreadyLogin) {
//       next('/')
//     } else {
//       next()
//     }
//   }
// })
// 每次路径改变，都会重新走一次导航守卫
router.beforeEach(async (to, from) => {
  const store = useMainStore()
  const { isLogin } = store.user
  const { token } = store
  if (!isLogin && token) {
    http.defaults.headers.common.Authorization = `Bearer ${token}`
    await getCurrentUser()
      .then((res) => {
        store.fetchUser(res)
      })
      .catch((e) => {
        console.log(e)
        store.logout()
      })
  }
})
router.beforeEach((to, from) => {
  const store = useMainStore()
  const { reuqiredLogin } = to.meta
  const { isLogin } = store.user
  if (reuqiredLogin && !isLogin) {
    return '/login'
  }
})
export default router
